package values.avatar
{
import values.common.SimplePlayerValue;
import values.events.AvatarValueEvent;
import values.item.ItemTemplateKeyType;
import values.item.ItemValue;
import values.item.ResourceItemValue;
import values.tech.PlayerTechValue;

/**
 * PlayerAvatarValue
 * @author jliang
 *
 */
public class PlayerAvatarValue extends AvatarValue implements IPlayerAvatarProperty
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function PlayerAvatarValue()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _basicInfo:SimplePlayerValue;
	
	public function get basicInfo():SimplePlayerValue
	{
		return _basicInfo;
	}
	
	protected var coinValue:ResourceItemValue;
	
	public function get coinResourceItem():ResourceItemValue
	{
		return coinValue;
	}
	
	protected var woodValue:ResourceItemValue;
	
	public function get woodResourceItem():ResourceItemValue
	{
		return woodValue;
	}
	
	protected var metalValue:ResourceItemValue;
	
	public function get metalResourceItem():ResourceItemValue
	{
		return metalValue;
	}
	
	public function set coin(value:Number):void
	{
		var oldValue:Number = coin;
		
		coinValue.num = value;
		coinValue.forceUpdate();
		
		var newValue:Number = coin;
		
		if (oldValue != newValue)
		{
			dispatchAvatarEvent(AvatarValueEvent.PROPERTY_CHANGE, { avatar:this, property:"coin", oldValue:oldValue, newValue:newValue });
		}
	}
	
	public function get coin():Number
	{
		return coinValue.num;
	}
	
	protected var cashValue:ResourceItemValue;
	
	public function get cashResourceItem():ResourceItemValue
	{
		return cashValue;
	}
	
	public function set cash(value:Number):void
	{
		var oldValue:Number = cash;
		
		cashValue.num = value;
		cashValue.forceUpdate();
		
		var newValue:Number = cash;
		
		if (oldValue != newValue)
		{
			dispatchAvatarEvent(AvatarValueEvent.PROPERTY_CHANGE, { avatar:this, property:"cash", oldValue:oldValue, newValue:newValue });
		}
	}
	
	public function get cash():Number
	{
		return cashValue.num;
	}
	
	public function set wood(value:Number):void
	{
		var oldValue:Number = coin;
		
		woodValue.num = value;
		woodValue.forceUpdate();
		
		var newValue:Number = coin;
		
		if (oldValue != newValue)
		{
			dispatchAvatarEvent(AvatarValueEvent.PROPERTY_CHANGE, { avatar:this, property:"wood", oldValue:oldValue, newValue:newValue });
		}
	}
	
	public function get wood():Number
	{
		return woodValue.num;
	}
	
	public function set metal(value:Number):void
	{
		var oldValue:Number = coin;
		
		metalValue.num = value;
		metalValue.forceUpdate();
		
		var newValue:Number = coin;
		
		if (oldValue != newValue)
		{
			dispatchAvatarEvent(AvatarValueEvent.PROPERTY_CHANGE, { avatar:this, property:"metal", oldValue:oldValue, newValue:newValue });
		}
	}
	
	public function get metal():Number
	{
		return metalValue.num;
	}
	
	protected var energyValue:ResourceItemValue;
	
	public function get energyResourceItem():ResourceItemValue
	{
		return energyValue;
	}
	
	public function set energy(value:Number):void
	{
		var oldValue:Number = energy;
		
		energyValue.num = value;
		energyValue.forceUpdate();
		
		var newValue:Number = energy;
		
		if (oldValue != newValue)
		{
			dispatchAvatarEvent(AvatarValueEvent.PROPERTY_CHANGE, { avatar:this, property:"energy", oldValue:oldValue, newValue:newValue });
		}
	}
	
	public function get energy():Number
	{
		return energyValue.num;
	}
	
	protected var _maxEnergy:Number = 0;
	
	public function set maxEnergy(value:Number):void
	{
		changeProperty("maxEnergy", value);
	}
	
	public function get maxEnergy():Number
	{
		return _maxEnergy;
	}
	
	protected var techPointValue:ResourceItemValue;
	
	public function get techPointResourceItem():ResourceItemValue
	{
		return techPointValue;
	}
	
	public function set techPoint(value:Number):void
	{
		var oldValue:Number = techPoint;
		
		techPointValue.num = value;
		techPointValue.forceUpdate();
		
		var newValue:Number = techPoint;
		
		if (oldValue != newValue)
		{
			dispatchAvatarEvent(AvatarValueEvent.PROPERTY_CHANGE, { avatar:this, property:"techPoint", oldValue:oldValue, newValue:newValue });
		}
	}
	
	public function get techPoint():Number
	{
		return techPointValue.num;
	}
	
	protected var reputationValue:ResourceItemValue;
	
	public function get reputationResourceItem():ResourceItemValue
	{
		return reputationValue;
	}
	
	public function set reputation(value:Number):void
	{
		var oldValue:Number = reputation;
		
		reputationValue.num = value;
		reputationValue.forceUpdate();
		
		var newValue:Number = techPoint;
		
		if (oldValue != newValue)
		{
			dispatchAvatarEvent(AvatarValueEvent.PROPERTY_CHANGE, { avatar:this, property:"reputation", oldValue:oldValue, newValue:newValue });
		}
	}
	
	public function get reputation():Number
	{
		return reputationValue.num;
	}
	//==========================================================================
	//  Overridden methods: AvatarValue
	//==========================================================================
	/**
	 *  
	 * @param template
	 * @param params 
	 * { 
	 *     basicInfo: SimplePlayerValue,
	 *     tech: [ [ 科技key, 科技等级, 是否开启 ], ..... ],
	 * 		coin:xx,
	 *     cash:xx,
	 *     wood:xx,
	 *     metal:xx,
	 *     energy: xx,
	 *     maxEnergy:xxx,
	 *     techPoint: xx,
	 *     reputation: xx,
	 *     level: xx,
	 * 		xp: xxx,
	 * 	   enhance: [ 0, 0, 0 ],    // [ 战斗强化, 特技强化, 元素强化 ]
	 *	   equipments: [  ],    // [ [ "12F0945", "equipment_1", { "level":1 } ] ]
	 * 		unlockedSlots: x,
	 *     skills: [ [ 1, 1 ], [ 2, 1 ], [ 3, 1 ] ],[ 技能key, 技能等级 ],.....
	 *      ultimateSkillLevel: -1 或者 1,2,3,4....
	 *      selectedSkill: 1
	 *  }
	 * 
	 */	
	override public function initialize(template:AvatarTemplateValue, tech:PlayerTechValue, params:*=null):void
	{
		_basicInfo = params.basicInfo;
		
		coinValue = ItemValue.createResource(ItemTemplateKeyType.COIN, params.coin);
		
		cashValue = ItemValue.createResource(ItemTemplateKeyType.CASH, params.cash);
		
		woodValue = ItemValue.createResource(ItemTemplateKeyType.WOOD, params.wood);
		
		metalValue = ItemValue.createResource(ItemTemplateKeyType.METAL, params.metal);
		
		energyValue = ItemValue.createResource(ItemTemplateKeyType.ENERGY, params.energy);
		
		techPointValue = ItemValue.createResource(ItemTemplateKeyType.TECH_POINT, params.techPoint);
		
		reputationValue = ItemValue.createResource(ItemTemplateKeyType.REPUTATION, params.reputation);
		
		_maxEnergy = params.maxEnergy;
		
		tech = new PlayerTechValue();
		tech.initialize(params.tech);
		
		super.initialize(template, tech, params);
	}
}
}



